local ClientOHOH =_G.ClientOHOH
local M = {}
local shopPanel = nil
local CallBackManager = ClientOHOH.load.CallBackManager()
local BigNumberHelper = ClientOHOH.load.BigNumberHelper()
local GeneralTool=_G.CommonOHOH.load.GeneralTool()
function Initsts()
    local ui ={
        egg =  {
            probability1 = "ctrl.limitedEgg.egg.list.probability1",
            probability2 = "ctrl.limitedEgg.egg.list.probability2",
            probability3 = "ctrl.limitedEgg.egg.list.probability3",
            probability4 = "ctrl.limitedEgg.egg.list.probability4",
            pet1 = "ctrl.limitedEgg.egg.list.pet1",
            pet2 = "ctrl.limitedEgg.egg.list.pet2",
            pet3 = "ctrl.limitedEgg.egg.list.pet3",
            pet4 = "ctrl.limitedEgg.egg.list.pet4",
            petInfo = {
                dialog = "ctrl.limitedEgg.Dialog.dialog",
                des = "ctrl.limitedEgg.Dialog.des"
            }
        },
        weapon= {},
        OPPet = {
            powerText = "ctrl.OPPet.power",
            priceText = "ctrl.OPPet.price",
            buy = "ctrl.OPPet.buy"
        },
        gSideTag = "ctrl.sideBar.limited"
    }
    for i=1,3 do
        ui.weapon[i] = {
            cell = "ctrl.weapon."..i..".btn",
            cost = "ctrl.weapon."..i..".cost",
            expText = "ctrl.weapon."..i..".exp"
        }
    end
    local listName = {
        "x1",
        "x10",
        "x3"
    }
    for _, value in pairs(listName) do
        ui.egg[value] = {
            buyBtn = "ctrl.limitedEgg.egg."..value..".btn",
            price = "ctrl.limitedEgg.egg."..value..".price"
        }
    end
   return ui
end

function M.GetInitUiList()
    return Initsts()
end

function M.Init(ShopPanel)
    shopPanel = ShopPanel
    CallBackManager:AddRbxConnect(shopPanel,shopPanel.insts.limited.gSideTag.Activated,function()
        shopPanel:ScrollingGo(shopPanel.insts.limited.gSideTag.X.Value)
    end)
end

function M.Full(data)
    local limited = shopPanel.insts.limited
    local weaponIndex = 0
    for index, value in pairs(data) do
        if value.belong == "limitedEgg" then
           M.FullEgg(limited,value)
        end
        if value.belong == "op" then
            M.FullOP(limited,value)
        end
        if value.belong == "weapon" then
            weaponIndex = weaponIndex+1
            M.FullWeapon(limited,value,weaponIndex)
        end
    end
end

function M.FullEgg(limited,value)
    limited.egg["x1"].price.Text = value["priceX1"]
    limited.egg["x3"].price.Text =  value["priceX3"]
    limited.egg["x10"].price.Text =  value["priceX10"]
    local percentage =GeneralTool.GetProbabilityShowPercent(value.egg.probability)
    local mult = 100
    limited.egg.probability1.Text = math.floor(percentage[1] * mult + 0.5) / mult.."%"
    limited.egg.probability2.Text = math.floor(percentage[2] * mult + 0.5) / mult.."%"
    limited.egg.probability3.Text = math.floor(percentage[3] * mult + 0.5) / mult.."%"
    limited.egg.probability4.Text = math.floor(percentage[4] * mult + 0.5) / mult.."%"
    local buyList = {
        "x1",
        "x3",
        "x10"
    }

    for i, vBuy in pairs(buyList) do
        CallBackManager:AddRbxConnect(shopPanel,limited.egg[vBuy].buyBtn.Activated,function()
            print("value",value.buyInfo)
            shopPanel.ShopBuyEvent:Action(value.buyInfo[i])
        end)
    end

    local petList = {
        "pet1",
        "pet2",
        "pet3",
        "pet4"
    }

    for index, v in pairs(petList) do
        CallBackManager:AddRbxConnect(shopPanel,limited.egg[v].MouseEnter,function()
            M.ShowPetInfo(limited.egg[v],limited,value.egg.PetList[index])
        end)
        CallBackManager:AddRbxConnect(shopPanel,limited.egg[v].MouseLeave, function()
            M.ClosePetInfo()
        end);
    end

end


function M.FullOP(limited,value)
    limited.OPPet.priceText.Text = value.price
    shopPanel:FullText(limited.OPPet.powerText,{BigNumberHelper:ToString(value.petData.value)})
    CallBackManager:AddRbxConnect(shopPanel,limited.OPPet.buy.Activated,function()
        shopPanel.ShopBuyEvent:Action(value.buyInfo)
    end)
end

function M.FullWeapon(limited,value,weaponIndex)
    limited.weapon[weaponIndex].cost.Text = value.price
    shopPanel:FullText(limited.weapon[weaponIndex].expText,{BigNumberHelper:ToString(value.power)})
    CallBackManager:AddRbxConnect(shopPanel,limited.weapon[weaponIndex].cell.Activated,function()
        shopPanel.ShopBuyEvent:Action(value.buyInfo)
    end)
end
function M.ShowPetInfo(root,limited,pet)
    limited.egg.petInfo.dialog.Visible = true
    local abPos=root.AbsolutePosition
    limited.egg.petInfo.dialog.Position= UDim2.fromOffset(abPos.X,abPos.Y)
    --因为精度问题不能直接乘以100
    local percentage = math.floor(pet.info.value * 100 + 0.5)
    limited.egg.petInfo.des.Text = "Pet is "..percentage .."% as strong as your best pets!"
end

function M.ClosePetInfo()
    shopPanel.insts.limited.egg.petInfo.dialog.Visible = false
end

function M.Get()
    return M
end

return M 